﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace JshRCSWebApi.Util.Transition
{
    public static class Transition
    {
        /// <summary>
        /// 获取对象所有的字段（剔除字段）
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="entity">实体</param>
        /// <param name="whereList">排除字段</param>
        /// <returns></returns>
        public static Dictionary<string, object> GetDicEliminate<T>(this T entity, List<string> whereList = null)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            Type type = typeof(T); //获取MyClass的类型信息
            PropertyInfo[] propertyArray = type.GetProperties(); //获取所有的公有属性

            foreach (PropertyInfo property in propertyArray)
            {
                var value = property.GetValue(entity, null);
                if (value != null && (whereList == null || !whereList.Contains(property.Name)))
                {
                    dic.Add(property.Name, value);
                }
            }
            return dic;
        }

        /// <summary>
        /// 获取对象所有的字段（使用字段）
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="entity">实体</param>
        /// <param name="whereList">使用字段</param>
        /// <returns></returns>
        public static Dictionary<string, object> GetDicEmploy<T>(this T entity, List<string> whereList = null)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            Type type = typeof(T); //获取MyClass的类型信息
            PropertyInfo[] propertyArray = type.GetProperties(); //获取所有的公有属性

            foreach (PropertyInfo property in propertyArray)
            {
                var value = property.GetValue(entity, null);
                if (value != null && (whereList == null || whereList.Contains(property.Name)))
                {
                    dic.Add(property.Name, value);
                }
            }
            return dic;
        }

        /// <summary>
        /// 获取空格
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static string GetNodeSymbol(int num, bool nb)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < num; i++)
            {
                if (nb)
                {
                    sb.Append("&nbsp;&nbsp;");
                }
                else
                {
                    sb.Append("  ");
                }
            }

            return num > 1? sb + "└":"";
        }

        /// <summary>
        ///  从SQL语句里面解析出写入，更新，删除的表名
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static List<string> GetTables(string sql)
        {
            var insert = @"[\s\t]*INSERT [\s\t\r\n]*(INTO|)[\s\t\r\n]*[A-Za-z0-9_.""\[\]]*[\s\t\r\n]*";
            var update = @"[\s\t]*UPDATE [\s\t\r\n]*(SET|)[\s\t\r\n]*[A-Za-z0-9_.""\[\]]*[\s\t\r\n]*";
            var delete = @"[\s\t]*DELETE *(FROM|)[\s\t\r\n]*(WHERE|)[\s\t\r\n]*[A-Za-z0-9_.""\[\]]*[\s\t\r\n]*";
            List<string> list = new List<string>();
            var m = Regex.Matches(sql, insert, RegexOptions.IgnoreCase);
            foreach (Match item in m)
            {
                list.Add(item.Value.Trim());
            }
            var m2 = Regex.Matches(sql, update, RegexOptions.IgnoreCase);
            foreach (Match item in m2)
            {
                list.Add(item.Value.Trim());
            }
            var m3 = Regex.Matches(sql, delete, RegexOptions.IgnoreCase);
            foreach (Match item in m3)
            {
                list.Add(item.Value.Trim());
            }
            return list.Select(x => { return x.Substring(x.LastIndexOf(' ')).Trim(); }).Distinct().ToList();
        }
    }
}
